import React from 'react';
import routes, {onRouteBefore} from "./routers";
import {BrowserRouter, Route} from "react-router-dom";
import {setLocalStorage, getLocalStorage} from "./utils";
import store, {actions} from './redux';
import RouterGuard from './react-router-dom6-guard';
// import ReactRouterDom6Guard ,  {
//     FunctionRule
// } from 'react-router-dom6-guard';
import Loading from './components/Loading';

const App = function () {
    const UserInfo: any = getLocalStorage('userInfo');
    const access_token: any = getLocalStorage('access_token');
    if (UserInfo) {
        store.dispatch(actions.setUserInfo(JSON.parse(UserInfo)));
        store.dispatch(actions.setToken(access_token));
    }
    window.addEventListener('beforeunload', (event) => {
        setLocalStorage('userInfo', JSON.stringify(store.getState().userInfo));
        setLocalStorage('access_token', store.getState().access_token);
    });
    const baseUrl = `${process.env.PUBLIC_URL}` || '/'; //路由前缀 = 打包路径 如：app
    return (
        <BrowserRouter basename={baseUrl}>
            <RouterGuard
                routers={routes}
                onRouterBefore={onRouteBefore}
                loading={<Loading/>}
            />
        </BrowserRouter>
    );
};
export default App;
